package com.itheima.web;

import com.itheima.R;
import com.itheima.pojo.UmsMenu;
import com.itheima.pojo.UmsResource;
import com.itheima.pojo.UmsRole;
import com.itheima.service.UmsRoleService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-10:09
 */
@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService roleService;


    /**
     * 给角色分配菜单
     */
    @PostMapping("/allocMenu")
    public R allocMenu(@RequestParam Integer[] menuIds, @RequestParam Integer roleId){
        roleService.allocMenu(menuIds, roleId);
        return R.success(null);
    }


    /**
     * 给角色分配资源
     */
    @PostMapping("/allocResource")
    public R allocResource(@RequestParam Integer[] resourceIds, @RequestParam Integer roleId){
        roleService.allocResource(resourceIds, roleId);
        return R.success(null);
    }


    /**
     * 添加角色
     */
    @PostMapping("/create")
    public R createRole(@RequestBody UmsRole role){
        roleService.createRole(role);
        return R.success(null);
    }


    /**
     * 批量删除角色
     */
    @PostMapping("/delete")
    public R deleteMulti(@RequestParam Integer[] ids){
        roleService.deleteMulti(ids);
        return R.success(null);
    }


    /**
     * 根据角色名称分页获取角色列表
     */
    @GetMapping("/list")
    public R pageRoleByName(String keyword, Integer pageNum, Integer pageSize){
        PageResult<UmsRole> pageInfo = roleService.pageRoleByName(keyword, pageNum, pageSize);
        return R.success(pageInfo);
    }


    /**
     * 获取所有角色
     */
    @GetMapping("/listAll")
    public R listAllRole(){
        List<UmsRole> roleList = roleService.list();
        return R.success(roleList);
    }


    /**
     * 获取角色相关菜单
     */
    @GetMapping("/listMenu/{roleId}")
    public R getRoleRelatedMenu(@PathVariable("roleId") Integer roleId){
        List<UmsMenu> menuList = roleService.getRelatedMenu(roleId);
        return R.success(menuList);
    }


    /**
     * 获取角色相关资源
     */
    @GetMapping("/listResource/{roleId}")
    public R getRoleRelatedResource(@PathVariable("roleId") Integer roleId){
        List<UmsResource> resourceList = roleService.getRelatedResource(roleId);
        return R.success(resourceList);
    }


    /**
     * 修改角色
     */
    @PostMapping("/update/{id}")
    public R updateRoleById(@PathVariable("id") Integer roleId, @RequestBody UmsRole umsRole){
        roleService.updateById(roleId, umsRole);
        return R.success(null);
    }

    @PostMapping("/updateStatus/{id}")
    public R updateRoleStatus(@PathVariable("id")Integer roleId, Integer status){
        roleService.updateStatus(roleId, status);
        return R.success(null);
    }

}
